import React from 'react'
import {Router} from 'dva/router'
import App from './routes/app'

const cached = {};
const registerModel = (app, model) => {
  if (!cached[model.namespace]) {
    app.model(model);
    cached[model.namespace] = 1
  }
};

export default function ({history, app}) {
  const routes = [
    {
      path: '/',
      component: App,
      getIndexRoute (nextState, cb) {
        require.ensure([], require => {
          registerModel(app, require('./models/users'));
          cb(null, {component: require('./routes/users')})
        }, 'users')
      },
      childRoutes: [
        {
          path: 'dashboard',
          name: 'dashboard',
          getComponent (nextState, cb) {
            require.ensure([], require => {
              registerModel(app, require('./models/dashboard'));
              cb(null, require('./routes/dashboard'))
            }, 'dashboard')
          }
        }, {
          path: 'users',
          name: 'users',
          getComponent (nextState, cb) {
            require.ensure([], require => {
              registerModel(app, require('./models/users'));
              cb(null, require('./routes/users'))
            }, 'users')
          }
        }, {
          path : 'roles',
          name : 'roles',
          getComponent(nextState, cb){
            require.ensure([], require =>{
              registerModel(app, require('./models/roles'));
              cb(null, require('./routes/roles'))
            }, 'roles')
          }
        },{
          path : 'userRoles',
          name : 'userRoles',
          getComponent(nextState, cb){
            require.ensure([], require =>{
              registerModel(app, require('./models/userRoles'));
              cb(null, require('./routes/userRoles'))
            }, 'userRoles')
          }
        }, {
          path : 'nodeRoles',
          name : 'nodeRoles',
          getComponent(nextState, cb){
            require.ensure([], require =>{
              registerModel(app, require('./models/nodeRoles'));
              cb(null, require('./routes/nodeRoles'))
            }, 'nodeRoles')
          }
        },{
          path : 'costs',
          name : 'costs',
          getComponent(nextState, cb){
            require.ensure([], require=>{
              registerModel(app, require('./models/costs'));
              cb(null, require('./routes/costs'));
            }, 'costs')
          }
        },{
          path : 'nodes',
          name : 'nodes',
          getComponent(nextState, cb){
            require.ensure([], require=>{
              registerModel(app, require('./models/nodes'));
              cb(null, require('./routes/nodes'));
            }, 'nodes')
          }
        }, {
          path : 'sdv',
          name : 'sdv',
          getComponent(nextState, cb){
            require.ensure([], require=>{
              registerModel(app, require('./models/sdv'));
              cb(null, require('./routes/sdv'));
            }, 'sdv')
          }
        }, {
          path : 'rules',
          name : 'rules',
          getComponent(nextState, cb){
            require.ensure([], require=>{
              registerModel(app, require('./models/nodes'));
              cb(null, require('./routes/nodes'));
            }, 'rules')
          }
        }, {
          path: 'ui/ico',
          name: 'ui/ico',
          getComponent (nextState, cb) {
            require.ensure([], require => {
              cb(null, require('./routes/ui/ico'))
            }, 'ui-ico')
          }
        }, {
          path: 'ui/search',
          name: 'ui/search',
          getComponent (nextState, cb) {
            require.ensure([], require => {
              cb(null, require('./routes/ui/search'))
            }, 'ui-search')
          }
        }, {
          path: 'ui/dropOption',
          name: 'ui/dropOption',
          getComponent (nextState, cb) {
            require.ensure([], require => {
              cb(null, require('./routes/ui/dropOption'))
            }, 'ui-dropOption')
          }
        }, {
          path: '*',
          name: 'error',
          getComponent (nextState, cb) {
            require.ensure([], require => {
              cb(null, require('./routes/error'))
            }, 'error')
          }
        }
      ]
    }
  ];

  return <Router history={history} routes={routes} />
}
